<template>
	<view class="edit_card">
		<u-gap height="40rpx"></u-gap>
		<u-button color="#588BF3" shape="circle" text="+上传" customStyle="width: 244rpx;height: 68rpx;display: block;float: right;right:28rpx;" @click="upDateImg"></u-button>
		<image class="upload_img" :src="wechatQrCode" mode="aspectFit"></image>
	</view>
</template>

<script>
	import {editWechatQrCode} from '@/api/cardSalesman';
	import caches from '@/config/caches.js'
	import { baseUrl } from '@/config/minRequest.js';
	export default {
		data() {
			return{
				wechatQrCode:''
			}
		},
		onLoad(option) {
			this.wechatQrCode = option.wechatQrCode
		},
		methods:{
			upDateImg(){
				let _this = this;
				uni.chooseImage({ // 从本地相册选择图片或使用相机拍照。
					count: 1,
						sizeType: ["original", "compressed"],
						success: (res) => {
							console.log(res.tempFilePaths[0]); //成功则返回图片的本地文件路径列表 tempFilePaths
							_this.wechatQrCode = res.tempFilePaths[0];
							uni.uploadFile({
								url: `${baseUrl}/salesman/fileOss/uploadFileImg`,
								filePath: res.tempFilePaths[0], //图片地址
								header: {
									'Content-Type': 'multipart/form-data',
									Authorization: `Bearer ${uni.getStorageSync(caches.ACCESS_TOKEN)}` || ''
								},
								name: "file",
								success: (res) => {
									if (res.statusCode === 200) {
										let wechatQrCode = JSON.parse(res.data).data;
										setTimeout(() => {
											this.submitWechatQrCode(wechatQrCode);
										}, 500);
									} else {
										that.$tool.toast(res.message);
									}
								},
							});
						},
					});
				},
			//上传图片
			async submitWechatQrCode(wechatQrCode) {
				const res = await editWechatQrCode({
					wechatQrCode: wechatQrCode
				})
				if (res.code === 200) {
					uni.$u.toast('上传成功');
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.edit_card{
		background: #FFFFFF;
		width: 100%;
		height: 100vh;
		text-align: center;
	}
	.upload_img{
		// margin: 0 auto;
		width: 100%;
		height: 100%;
	}
</style>